package com.yhc.order.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);

        RedisSerializer<?> stringRS = new StringRedisSerializer();
        RedisSerializer<?> jacksonRS = new Jackson2JsonRedisSerializer<>(Object.class);

        template.setKeySerializer(stringRS);
        template.setHashKeySerializer(stringRS);

        template.setValueSerializer(jacksonRS);
        template.setHashValueSerializer(jacksonRS);
        
        template.afterPropertiesSet();
        return template;
    }
}
